<template>
  <div class="body-box">
    <div class="titlePage background-set">
      <el-row>
        <el-col :span="24" class="titlePage-title">
          <!-- <div>
            <span class="mid-title" @click="handleGoindex">
              公共机构双控双碳数字管家
            </span>
          </div> -->
          <div class="title-button-area">
            <div class="dept-light">
              <span>{{ userDeptname }}</span>
            </div>
            <img
              v-if="goBigscreenFalge"
              src="@/assets/images/nav/homebutton-big.png"
              alt=""
              @click="goBigscreen"
            />
            <img
              v-if="showBack"
              src="@/assets/images/nav/backbutton-big.png"
              alt=""
              @click="handleGoback"
            />
            <img
              src="@/assets/images/nav/closebutton-big.png"
              alt=""
              @click="handleLogout"
            />
          </div>
        </el-col>
      </el-row>
      <div class="mid-title-area">
        <!-- <div class="mid-title"> -->
        <span @click="handleGoindex">公共机构双控双碳数字管家</span>
        <!-- </div> -->
      </div>
      <div class="content-box">
        <div class="card-box">
          <div v-for="(item, index) in yourRouterArr" :key="index">
            <div class="card-item" v-if="item.showCard">
              <!-- @mouseenter="handlerMouseover(index)"
            @mouseleave="handlerMouseout(index)" -->
              <div
                :class="[
                  'card-item-content',
                  // 'card-item-content' + index,
                  // activeMenu == index ? 'card-item-content-active' : '',
                ]"
              >
                <!-- :style="
                'background: url(' +
                baseUrl +
                item.logImg2 +
                ') center no-repeat;'
              " -->
                <img class="back-image" :src="baseUrl + item.logImg2" alt="" />
                <!-- 正面内容 -->
                <div class="card-item-front">
                  <div class="cif-top">
                    <div class="cif-content">
                      <img :src="baseUrl + item.logImg1" alt="" />
                    </div>
                    <span class="cif-title">{{ item.name }}</span>
                    <!-- <span class="cif-subheading">{{ item.subheading }}</span> -->
                    <div
                      class="cif-router"
                      v-if="item.children && item.children.length > 0"
                    >
                      <div
                        class="cif-item"
                        v-for="(tig, tindex) in item.children"
                        :key="tindex"
                      >
                        <span v-if="tig.selected" @click="handlerGolink(tig)">
                          {{ tig.name }}
                        </span>
                      </div>
                    </div>
                  </div>
                  <!-- <div class="cif-bottom"> -->
                  <!-- <img
                    :src="
                      screenWidth <= '2880'
                        ? require('@/assets/images/nav/card-front/into-small.png')
                        : require('@/assets/images/nav/card-front/into-big.png')
                    "
                    alt=""
                  /> -->
                  <!-- @click="handlerGolink(item)" -->
                  <!-- </div> -->
                </div>
                <!-- 反面内容 -->
                <!-- <div class="card-item-behind">
                <div v-for="(tig, tindex) in item.route" :key="tindex">
                  <div
                    class="cib-item"
                    :style="tig.disabled ? 'display: none' : ''"
                  >
                    <img
                      :src="screenWidth <= '2880' ? tig.logo : tig.logo2"
                      alt=""
                    />
                    <div class="tig-name" @click="handleGoroute(tig)">
                      {{ tig.name }}
                    </div>
                  </div>
                </div>
              </div> -->
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
    <!-- 退出弹出层 -->
    <el-dialog
      title="提示"
      :visible.sync="dialogVisible"
      width="30%"
      :before-close="handleClose"
      class="logout-dialog"
    >
      <span>确定注销并退出系统吗？</span>
      <span slot="footer" class="dialog-footer">
        <el-button
          class="default-button"
          size="small"
          @click="dialogVisible = false"
          >取 消</el-button
        >
        <el-button class="primary-button" size="small" type="" @click="logout">
          确 定
        </el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
/*
* //! 路由集合中对象的属性作用解析
* {
    name: "left-1", // 名称
    showLine:true, // 显示tcc-line控制(如果跳转的route全是disabled为true,则导航菜单不显示,这个line也不必显示)
    type: 1, // 路由级别: 0 暂时未有跳转路由（disabled为true）; 1一级目录路由（例如首页）;  2 二级菜单路由;
    mother: "/index", // 母目录
    children: "", // 子菜单
    route: "/index", // 跳转路由
    special: false, // 是否有特殊跳转(比如跳转弹出层详情页)
    disabled: true, // 是否可点击
    tooltip: undefined, // 是否有文字提示 有：提示内容 无：undefined或者''
* }
*/
import { getRoutermenuList } from "@/api/navset/index.js";
export default {
  name: "TitlePage",
  dicts: [
    "sys_dept_type", // 部门类型
  ],
  components: {},
  data() {
    return {
      baseUrl: process.env.VUE_APP_BASE_API,
      goBigscreenFalge: false, // 能否去往大屏
      screenWidth: "", // 屏幕宽度
      screenHeight: "", // 屏幕高度
      dialogVisible: false,
      vedioCanPlay: false,
      activeMenu: null,
      roleArr: [],
      userDeptname: "",
      // leaveMenu: null,
      /* 选择展示的card集合 */
      choseShowArr: [],
      backTOindex: false,
      yourRouterArr: [],
      noCard: false, // 没有卡片可显示时跳转能跳转的第一个页面
      showBack: true,
    };
  },
  created() {
    // this.getRoutersarr();
  },
  mounted() {
    this.judgeGobig();
    this.requestMenulist();
    setTimeout(() => {
      this.getDpetrole();
    }, 300);
    this.screenWatcher();
  },
  beforeRouteEnter(to, from, next) {
    next((vm) => {
      if (from.path === "/") {
        // 从驾驶舱过来
        vm.backTOindex = true;
      }
    });
  },
  methods: {
    judgeGobig() {
      let roles = this.$store.state.user?.roles ?? [];
      if (roles.includes("admin")) {
        this.goBigscreenFalge = true;
      } else {
        this.goBigscreenFalge = false;
      }
    },
    requestMenulist() {
      this.yourRouterArr = [];
      getRoutermenuList(1)
        .then((res) => {
          if (res.code === 700) {
            this.$message.warning(res.msg);
            return false;
          }
          let arr = res?.data?.dynamicMenuConfigItemVos ?? [];
          this.yourRouterArr = [...arr];
          this.judgeCard();
        })
        .catch(() => {
          // this.$message.error("系统错误,请联系管理员!");
        });
    },
    // 判断是否显示这个card
    judgeCard() {
      let permissions = this.$store.state.user.permissions ?? [];
      if (permissions.length == 0) {
        this.$message.warning("您的角色未配置任何菜单内容，请与管理员确认！");
        this.showBack = false;
        return;
      } else {
        this.showBack = true;
        if (this.yourRouterArr.length > 0) {
          this.yourRouterArr.map((item) => {
            if (item.children && item.children.length > 0) {
              let num = 0;
              item.children.map((tig) => {
                if (!tig.selected) {
                  num++;
                }
              });
              if (num == item.children.length) {
                item.showCard = false;
              } else {
                item.showCard = true;
              }
            } else {
              item.showCard = false;
            }
          });
          // 判断是否没有路由可以跳转
          this.yourRouterArr.map((item) => {
            let num = 0;
            if (!item.showCard) {
              num++;
            }
            if (num == this.yourRouterArr.length) {
              this.noCard = true;
            }
          });
        } else {
          this.noCard = true;
        }
        this.nowayTogo();
      }
    },
    nowayTogo() {
      if (this.noCard) {
        this.$message.warning("您的角色没有对应的导航内容，正在为您跳转首页！");
        setTimeout(() => {
          this.$router.push({
            path: "/",
          });
        }, 500);
      }
    },
    handlerGolink(target) {
      // console.log(target);
      let routePath = target.route;
      let query = {};
      if (target.name == "驾驶舱") {
        //去往驾驶舱
        this.goBigscreen();
      } else {
        this.$router.push({ path: routePath, query: query }).catch(() => {});
      }
      // if (target.disableRole === 0) {
      // if (target.special != null) {
      //   // 查字典nav_special对query赋值操作
      //   if (target.special == 4) {
      //     //去往驾驶舱
      //     this.goBigscreen();
      //     return;
      //   }
      // }
      // this.$router.push({ path: routePath, query: query }).catch(() => {});
      // } else {
      //   this.$message.warning("此导航已暂时关闭");
      // }
    },
    // 浏览器视窗大小监控
    screenWatcher() {
      this.screenWidth = document.body.clientWidth;
      this.screenHeight = document.body.clientHeight;
      window.onresize = () => {
        return (() => {
          this.screenWidth = document.body.clientWidth;
          this.screenHeight = document.body.clientHeight;
        })();
      };
    },
    // 角色部门权限判断
    getDpetrole() {
      this.roleArr = [];
      let deptTypearr = this.dict.type?.sys_dept_type ?? [];
      if (deptTypearr.length > 0) {
        deptTypearr.map((item) => {
          let obj = {
            name: item.label,
            value: item.value,
            hasRole: false,
          };
          this.roleArr.push(obj);
        });
      }
      let roles = this.$store.state.user?.roles ?? [];
      let userDepttype = this.$store.state.user?.deptType ?? null;
      // console.log(roles, userDepttype, "roles&&userDepttype");
      if (this.roleArr.length > 0) {
        this.roleArr.map((item) => {
          if (item.value == userDepttype) {
            item.hasRole = true;
            this.userDeptname = item.name;
          }
          if (roles.length > 0) {
            if (roles.includes("admin")) {
              // 部门权限灯全开
              this.userDeptname = "超级管理员";
              item.hasRole = true;
            }
          }
        });
      }
      // console.log(this.roleArr, "roleArr");
    },
    //! 路由匹配判断
    // routeMatching(allroute, arr) {
    //   if (allroute.length > 0) {
    //     allroute.map((item) => {
    //       arr.map((tig) => {
    //         if (tig.type == 0) {
    //           tig.disabled = true;
    //         } else if (tig.type == 1) {
    //           if (item.path == tig.mother) {
    //             tig.disabled = false;
    //           }
    //         } else if (tig.type == 2) {
    //           if (item.path == tig.mother) {
    //             if (item.children.length > 0) {
    //               item.children.map((pig) => {
    //                 if (pig.path == tig.children) {
    //                   tig.disabled = false;
    //                 }
    //               });
    //             }
    //           }
    //         }
    //       });
    //     });
    //   }
    // },
    // 菜单路由跳转
    // handleGoroute(tig) {
    //   if (tig.special) {
    //     let query = {};
    //     switch (tig.name) {
    //       // case "节能降碳标准":
    //       //   query = {
    //       //     sortFiled: "1",
    //       //   };
    //       //   break;
    //       case "双控核查窗口":
    //         query = {
    //           choseOne: "1",
    //         };
    //         break;
    //     }
    //     this.$router.push({ path: tig.route, query: query }).catch(() => {});
    //   } else {
    //     this.$router.push(tig.route).catch(() => {});
    //   }
    // },
    /* 去往首页暂时 */
    handleGoindex() {
      if (this.$store.state.user.id === 1) {
        this.$router.push("/work/general").catch(() => {});
      }
    },
    handleGoback() {
      if (this.backTOindex) {
        this.$router.push("/work/general").catch(() => {});
      } else {
        this.$router.go(-1);
      }
    },
    handleLogout() {
      this.dialogVisible = true;
    },
    handleClose() {
      this.dialogVisible = false;
    },
    async logout() {
      this.handleClose();
      this.$store.dispatch("LogOut").then(() => {
        location.href = "/work/general";
      });
    },
    // handlerMouseover(index) {
    //   this.activeMenu = index;
    // },
    // handlerMouseout(index) {
    //   this.activeMenu = null;
    // },
    canplay() {
      this.vedioCanPlay = true;
    },
    /* 去往大屏 */
    goBigscreen() {
      if (process.env.NODE_ENV === "development") {
        let token = localStorage.getItem("t-token");
        window.location = process.env.VUE_APP_SIP + "?token=" + token;
        // window.open(process.env.VUE_APP_SIP);
      } else {
        // 生产环境待处理
        window.open(this.$router.resolve({ path: "/dt" }).href, "_parent");
      }
    },
  },
};
</script>

<style lang="scss" scoped>
::v-deep .logout-dialog {
  .el-dialog {
    top: 250px;
    background: linear-gradient(
      to bottom,
      rgba($color: #0a537e, $alpha: 1) 0%,
      rgba($color: #29536b, $alpha: 0.7) 50%,
      rgba($color: #838282, $alpha: 0.5) 100%
    );

    .el-dialog__header {
      padding: 10px 0;
      margin: 0;
      position: relative;

      .el-dialog__title {
        padding-left: 15px;
        color: #fad42c;
      }

      .el-icon-close {
        position: absolute;
        font-size: 22px;
        color: #fad42c;
        top: -30px;
        left: 20px;
      }
    }

    .el-dialog__body {
      padding: 0;
      margin: 0;
      padding: 10px 0 10px 15px;
      // color: #000000;
      color: #fad42c;
    }

    .el-dialog__footer {
      padding: 0;
      margin: 0;
      padding: 10px 0;

      .el-button {
        &:nth-child(2) {
          margin-right: 15px;
        }
      }
    }
  }

  .default-button {
    border: none;
    background: #ffffff;

    &:hover {
      color: #fad42c;
    }
  }

  .primary-button {
    border: #29536b solid 1px;
    background: #29536b;
    color: #ffffff;

    &:hover {
      color: #fad42c;
    }
  }
}

::v-deep .dbcarbon-dialog {
  // margin-top: -100px;
  .el-dialog {
    padding: 0;
    background: rgba($color: #000000, $alpha: 0);
    .el-dialog__header {
      display: none;
    }
    .el-dialog__body {
      padding: 0;
      margin: 0;
    }
  }
  .dbcarbon-dialog-content {
    background: rgba($color: #000000, $alpha: 0);
    position: relative;
    .ddc-top {
      z-index: 98;
      position: relative;
      .title-area {
        position: absolute;
        left: 18px;
        top: 24px;
        /* 标题 */
        width: 501px;
        height: 41px;
        background: url("~@/assets/images/nav/tab-chose1/tab-chose-title-small.png")
          center no-repeat;
        z-index: 98;
        display: flex;
        align-items: center;

        span {
          margin-left: 20px;
          font-size: 24px;
          font-family: PingFangSC-Semibold, PingFang SC;
          font-weight: 600;
          color: #ffffff;
          line-height: 33px;
        }
      }

      .icon-area {
        cursor: pointer;
        position: absolute;
        top: 18px;
        right: 18px;
        width: 24px;
        height: 24px;
        background-color: #04bbe9;
        display: flex;
        align-items: center;
        justify-content: center;
        .el-icon-close {
          font-size: 22px;
          color: #ffffff;
        }
      }
    }
    .ddc-content {
      width: 100%;
      height: 100%;
      position: relative;
      .video-area {
        width: 100%;
        height: 100%;
        position: absolute;
        z-index: 1;
      }
    }
  }
  .ddc-small {
    width: 1136px;
    height: 681px;
  }
  .ddc-big {
    width: 1952px;
    height: 853px;
  }
}
.dbcarbon-dialog-big {
  margin-top: -70px;
}
@import "~@/assets/styles/mixin.scss";

@media screen and (min-width: 0px) and (max-width: 2880px) {
  @import "~@/assets/styles/titlepage-small-new.scss";
  .background-set {
    width: 100%;
    height: 100%;
    overflow: hidden;
    margin: 0 auto;
    background: url("~@/assets/images/nav/navBackground-small.png") !important;
    background-position: center bottom;
    background-repeat: no-repeat;
    background-size: cover;
    background-blend-mode: luminosity;
  }
}
@media screen and (min-width: 2881px) {
  @import "~@/assets/styles/titlepage-big-new.scss";
  .background-set {
    width: 100%;
    height: 100%;
    overflow: hidden;
    margin: 0 auto;
    background: url("~@/assets/images/nav/navBackground-big.png") !important;
    background-position: center bottom;
    background-repeat: no-repeat;
    background-size: cover;
    // animation: colorswitch 2s infinite;
    background-blend-mode: luminosity;
  }
}
</style>
